/**
 * 动态import工具类
 */
import {configChange} from "../Config";


import {log} from "./AppUtil";

let project: any, projectName: string;

configChange((config: any) => {
    project = config.project;
    projectName = project ? `/${project.name}` : "";
});

/**
 * 动态获取组件对象
 * @param path 以component为根目录的地址 如 /login,取指定文件中子组件使用'.'  如 /common/ListPage.SearchForm
 * @returns {*}
 */
// @ts-ignore
const importComponent: Function = async (path: string) => {
    if (!path) {
        return null;
    }
    let result: any, splitList: string[] = path.split(".");
    if (splitList[0].substr(0, 1) === "/") {
        let prevPath: string = splitList[0];
        let modules = (await import.meta.glob('../component/**/*.tsx'));
        //获取
        // @ts-ignore
        result = (await (modules[`../component${prevPath}.tsx`])()).default;

    } else {
        log(`按照命名空间import暂未开发，没有找到'${path}'组件`);
        return null
    }
    if (splitList.length > 1) {
        // 如果有 '.' 分隔符 则从import到的对象中继续获取目标对象
        result = [result, ...splitList.splice(1)].reduce((obj, item) => obj[item]);
    }
    return result;

};

export {
    importComponent
};